package com.atguigu.lease.web.admin.service.impl;

import com.atguigu.lease.common.exception.LeaseException;
import com.atguigu.lease.common.result.ResultCodeEnum;
import com.atguigu.lease.model.entity.*;
import com.atguigu.lease.model.enums.LeaseStatus;
import com.atguigu.lease.web.admin.mapper.LeaseAgreementMapper;
import com.atguigu.lease.web.admin.mapper.RoomInfoMapper;
import com.atguigu.lease.web.admin.service.*;
import com.atguigu.lease.web.admin.vo.agreement.AgreementQueryVo;
import com.atguigu.lease.web.admin.vo.agreement.AgreementVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;

/**
 * @author liubo
 * @description 针对表【lease_agreement(租约信息表)】的数据库操作Service实现
 * @createDate 2023-07-24 15:48:00
 */
@Service
public class LeaseAgreementServiceImpl extends ServiceImpl<LeaseAgreementMapper, LeaseAgreement>
        implements LeaseAgreementService {
    @Autowired
    private LeaseAgreementMapper leaseAgreementMapper;
    @Autowired
    private ApartmentInfoService apartmentInfoService;
    @Autowired
    private RoomInfoMapper roomInfoMapper;
    @Autowired
    private PaymentTypeService paymentTypeService;
    @Autowired
    private LeaseTermService leaseTermService;

    /**
     * 分页查询租约信息
     * @param page
     * @param queryVo
     * @return
     */
    @Override
    public IPage<AgreementVo> pageItem(IPage<AgreementVo> page, AgreementQueryVo queryVo) {
        return leaseAgreementMapper.pageItem(page,queryVo);
    }

    /**
     * 根据id查询租约信息
     * @param id
     * @return
     */
    @Override
    @Transactional
    public AgreementVo selectById(Long id) {
        LeaseAgreement agreement = this.getById(id);
        AgreementVo agreementVo = new AgreementVo();
        BeanUtils.copyProperties(agreement,agreementVo);

        //查询公寓信息
        LambdaQueryWrapper<ApartmentInfo> apartmentInfoLambdaQueryWrapper = new LambdaQueryWrapper<>();
        apartmentInfoLambdaQueryWrapper.eq(ApartmentInfo::getId,agreement.getApartmentId());
        ApartmentInfo apartmentInfo = apartmentInfoService.getOne(apartmentInfoLambdaQueryWrapper);
        agreementVo.setApartmentInfo(apartmentInfo);

        //查询房间信息
        LambdaQueryWrapper<RoomInfo> roomInfoLambdaQueryWrapper = new LambdaQueryWrapper<>();
        roomInfoLambdaQueryWrapper.eq(RoomInfo::getId,agreement.getRoomId());
        RoomInfo roomInfo = roomInfoMapper.selectOne(roomInfoLambdaQueryWrapper);
        agreementVo.setRoomInfo(roomInfo);

        //查询支付方式
        LambdaQueryWrapper<PaymentType> paymentTypeLambdaQueryWrapper = new LambdaQueryWrapper<>();
        paymentTypeLambdaQueryWrapper.eq(PaymentType::getId,agreement.getPaymentTypeId());
        PaymentType paymentType = paymentTypeService.getOne(paymentTypeLambdaQueryWrapper);
        agreementVo.setPaymentType(paymentType);

        //查询租期
        LambdaQueryWrapper<LeaseTerm> leaseTermLambdaQueryWrapper = new LambdaQueryWrapper<>();
        leaseTermLambdaQueryWrapper.eq(LeaseTerm::getId,agreement.getLeaseTermId());
        LeaseTerm leaseTerm = leaseTermService.getOne(leaseTermLambdaQueryWrapper);
        agreementVo.setLeaseTerm(leaseTerm);


        return agreementVo;
    }

    @Override
    public void deleteByid(Long id) {

        //判断 租约中状态是否为 （2，5，7）或 还没有到 租约结束日期 若是 就不能删除
        LeaseAgreement leaseAgreement = leaseAgreementMapper.selectById(id);

        LeaseStatus status = leaseAgreement.getStatus();
        Date endDate = leaseAgreement.getLeaseEndDate();
        int compare = endDate.compareTo(new Date());
        if ((status== LeaseStatus.SIGNED || status == LeaseStatus.WITHDRAWING || status == LeaseStatus.RENEWING) && compare>=0){
            throw new LeaseException(ResultCodeEnum.ADMIN_LEASE_AGREEMENT_EXISTS);
        }

        this.removeById(id);
    }
}




